package com.bigshen.springcloud.orderserviceprovider.controller.dto;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.validation.ValidationException;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@Data
//@Getter
//@Setter
public class OrderDto {

    @NotNull(message = "name不能为空")
    private String name;
    @NotNull(message = "ntel不能为空")
    private String tel;

    private String userId;
    @NotEmpty(message = "name不能为空")
    private List<ItemDto> items;


    public void validData(BindingResult bindingResult) {

        if(bindingResult.hasErrors()) {
            StringBuilder stringBuilder = new StringBuilder();
            for(ObjectError oe : bindingResult.getAllErrors()) {
                stringBuilder.append(oe.getDefaultMessage() + "\n");
            }
            throw new ValidationException(stringBuilder.toString());
        }

    }


}
